library(data.table)
library(ggplot2)
library(lfe)
library(Hmisc)
library(stargazer)
library(scales) 




bd <- fread('../data/bankdata_conventional.csv')        # Bank capitalization data


d.in[, held_share := held_volume/total_volume*100]
d.in[,jumbo_share := jumbo_volume/total_volume*100]
d.in[,conf_held := conforming_held_volume/conforming_volume*100]

#drop the rows with missing loan amount
d.in <- d.in[!is.na(d.in$loan_amount),]

#standardization
d.in[,z_cr := scale(cr)]
d.in[,z_cr_gap := scale(cr_gap)]
d.in[,z_log_volume := scale(log(total_volume))]
d.in[,z_log_cts := scale(log(unique_cts))]
d.in[,z_log_assets := scale(log(total_assets))]
d.in[,z_log_income := scale(log(income))]
d.in[,z_log_loan_amount := scale(log(loan_amount))]
d.in[,z_deposit_ratio := scale(deposit_ratio)]
d.in[,z_non_core := scale(noncore_funding)]
d.in[,z_core := scale(core_deposits)]

#Appendix A2 Panel A
a2.a.1 <- felm(held_share ~ z_cr + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year, data = d.in, weights = d.in$total_volume)
a2.a.2 <- felm(held_share ~ z_cr + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year + rssdid, data = d.in, weights = d.in$total_volume)
a2.a.3 <- felm(jumbo_share ~ z_cr + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year, data = d.in, weights = d.in$total_volume)
a2.a.4 <- felm(jumbo_share ~ z_cr + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year + rssdid, data = d.in, weights = d.in$total_volume)
a2.a.5 <- felm(conf_held ~ z_cr + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year, data = d.in, weights = d.in$conforming_volume)
a2.a.6 <- felm(conf_held ~ z_cr + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year + rssdid, data = d.in, weights = d.in$conforming_volume)

stargazer(a2.a.1,a2.a.2,a2.a.3,a2.a.4,a2.a.5,a2.a.6,  type = 'html')

#Appendix A2 Panel B
a2.b.1 <- felm(held_share ~ z_cr_gap + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year, data = d.in, weights = d.in$total_volume)
a2.b.2 <- felm(held_share ~ z_cr_gap + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year + rssdid, data = d.in, weights = d.in$total_volume)
a2.b.3 <- felm(jumbo_share ~ z_cr_gap + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year, data = d.in, weights = d.in$total_volume)
a2.b.4 <- felm(jumbo_share ~ z_cr_gap + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year + rssdid, data = d.in, weights = d.in$total_volume)
a2.b.5 <- felm(conf_held ~ z_cr_gap + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year, data = d.in, weights = d.in$conforming_volume)
a2.b.6 <- felm(conf_held ~ z_cr_gap + z_log_volume + z_log_cts + z_log_assets + z_log_income + z_log_loan_amount + z_deposit_ratio + z_non_core + z_core|year + rssdid, data = d.in, weights = d.in$conforming_volume)

stargazer(a2.b.1,a2.b.2,a2.b.3,a2.b.4,a2.b.5,a2.b.6,  type = 'html')